package com.bridgeintelligent.tag.webserver.dim.mgmt.service.export.excel;

import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimItem;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimPage;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.export.DimAggData;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.export.DimCompareData;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.export.ExcelCatalogModel;
import com.wayneleo.quickstart.framework.IDGenerator;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JackCheung
 * @date 2020/7/14 5:32 PM.
 * description:
 */
public interface IDimExcelMgmtService extends IDimExcelService {

    String FILE_NAME = "标签模型";
    String FILE_FULL_NAME = "标签模型.xlsx";

    /**
     * 下载上次上传的模版
     *
     * @param response HttpServletResponse
     */
    void downLoad(HttpServletResponse response, HttpServletRequest request);

    /**
     * 确认上传
     */
    void confirm();

    /**
     * 获取上传的标签列表
     *
     * @param pageNumber pageNumber
     * @param pageSize   pageSize
     * @param type       0为新增、1为更新
     * @return
     */
    DimPage<DimAggData> list(Integer pageNumber, Integer pageSize, String type,String dimName,String classify,String startTime,String endTime,String catalogId);

    /**
     * 获取excel中的标签和库中的标签区别
     *
     * @param dimId dimId
     * @return DimCompareData
     * @throws IllegalAccessException IllegalAccessException
     */
    DimCompareData detail(String dimId) throws IllegalAccessException;

    /**
     * 批量替换
     *
     * @param dimIds dimIds
     */
    void batchReplace(String[] dimIds);

    /**
     * 全部替换
     */
    void replaceAll();

    default byte[] inputStream2ByteArray(String filePath) throws IOException {
        InputStream in = new FileInputStream(filePath);
        byte[] data = toByteArray(in);
        in.close();
        return data;
    }

    default byte[] toByteArray(InputStream in) throws IOException {
        byte[] buffer = new byte[1024];
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int n;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        return out.toByteArray();
    }

    default List<DimItem> converToDimitem(List<DimAggData> list) {
        return list.stream()
                   .filter(dimAggData -> StringUtils.isNotEmpty(dimAggData.getItemName()) && StringUtils.isNotEmpty(dimAggData.getItemNo()))
                   .map(dimAggData -> {
                       DimItem dimItem = new DimItem();
                       dimItem.setItemName(dimAggData.getItemName());
                       dimItem.setDimId(dimAggData.getDimId());
                       dimItem.setId(IDGenerator.gen());
                       dimItem.setItemNo(dimAggData.getItemNo());
                       dimItem.setUpdateDate(fetchDate());
                       return dimItem;
                   })
                   .collect(Collectors.toList());
    }

    DateTimeFormatter YYYY_MM = DateTimeFormatter.ofPattern("yyyyMM");

    static String fetchDate() {
        return LocalDateTime.now()
                            .format(YYYY_MM);
    }


    void replaceByType(String type);

    List<ExcelCatalogModel> findSecondCatalog();
}
